// Copyright (C) 2008 Taylor L. Riche <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#ifndef BASICTIMER_H_
#define BASICTIMER_H_

#include <sys/time.h>
#include <iostream>

namespace timer
{

  const unsigned int NSEC_TO_USEC = 1000;

  class BasicTimer
  {
    //timespec start_val, end_val;
    timeval start_val, end_val;
    bool counting;
	
  public:
    BasicTimer() {}
    void start()
    {
      //clock_gettime(CLOCK_MONOTONIC, &start_val);
      gettimeofday(&start_val, NULL);
      counting = true;
      return;
    }
    double stop()
    {
      double retval = 0;
      if(counting) { 
/* 	clock_gettime(CLOCK_MONOTONIC, &end_val); */
/* 	std::cerr << end_val.tv_nsec << ":" << start_val.tv_nsec << std::endl; */
/* 	retval = static_cast<double>((((end_val.tv_sec * 1000000000) + end_val.tv_nsec) - ((start_val.tv_sec * 1000000000) + start_val.tv_nsec)) / NSEC_TO_USEC); */
	gettimeofday(&end_val, NULL);
 	retval = static_cast<double>(((end_val.tv_sec * 1000000) + end_val.tv_usec) - ((start_val.tv_sec * 1000000) + start_val.tv_usec));
	counting = false;
      }
      else {
	retval = -1;
      }
      return retval;
    } 
    virtual ~BasicTimer() {}
  };

}

#endif /*BASICTIMER_H_*/
